from hbase import Hbase
from hbase.ttypes import ColumnDescriptor
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
import setting

import HTable

import Eblob.HTable


def get_client():
    transport = TSocket.TSocket(setting.host, setting.port)
    protocol = TBinaryProtocol.TBinaryProtocol(transport)
    client = Hbase.Client(protocol)
    transport.open()
    return client


def create_table(table_name, columns_str):
    columns = []
    for i in columns_str:
        columns.append(ColumnDescriptor(name=i + ':'))
    get_client().createTable(table_name, columns)


def delete_table(table_name):
    get_client().deleteTable(table_name)


def change_table_name():
    pass


def create_column_family():
    pass


def delete_column_family():
    pass


def change_column_family_name():
    pass


def create_column():
    pass


def delete_column():
    pass


def change_column_name():
    pass


def test():
    for table_name in dir(setting.table_file):
        row = getattr(HTable.Tables, table_name)
        if type(row) == type(Eblob.HTable.BaseTable.Table) and row.__name__ != 'Table':
            contents = []
            for i in row.get_all_family():
                contents.append(ColumnDescriptor(name=i + ':'))
            get_client().createTable(table_name, contents)
            print 'Hbase sync success'
